/*
 * chunk.c
 *
 *  Created on: Mar 2, 2013
 *      Author: redchen
 */

#include <lfs.h>
#include <store_file.h>
#include <emalloc.h>
#include "chunk.h"

struct _Chunk_
{
    int64_t id; //chunk handle
    StoreFile* file;
};

Chunk* chunk_create(const char* path, int64_t id, const char* filename, int32_t size)
{
    Chunk* chunk = emalloc(sizeof(Chunk));
    chunk->id = id;
    chunk->file = store_file_create(path, filename, STORE_FILE_BLOCK, size);
    if(chunk->file == NULL)
    {
        efree(chunk);
        return NULL;
    }
    return chunk;
}

Chunk* chunk_load(const char* path, int64_t id)
{
    Chunk* chunk = emalloc(sizeof(Chunk));
    chunk->id = id;
    chunk->file = store_file_open(path);
    return chunk;
}

int chunk_append(Chunk* chunk, void* data, int32_t size)
{
    return store_file_wirte(chunk->file, data, size);
}

int chunk_read(Chunk* chunk, int64_t start, int64_t end, /*out:Vector<BFile>*/Vector* bfiles)
{
    return store_file_read(chunk->file, start, end, bfiles);
}

int32_t chunk_get_free_size(Chunk* chunk)
{
    return store_file_allocated_size(chunk->file) - store_file_used_size(chunk->file);
}

int32_t chunk_get_status(Chunk* chunk)
{
    return store_file_get_status(chunk->file);
}

void chunk_set_status(Chunk* chunk, int32_t status)
{
    store_file_set_status(chunk->file, status);
}
